<?php
namespace Modules\User\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\User\Entities\UserGroupPrice;

use \Modules\Product\Entities\Product;

use Validator;
use ThrowException;
use DB;

class UserGroupPriceController extends Controller
{
    // 获取人群价
    public function index(Request $req, $type, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest('请正确填写数量');

        if ($type === 'products') {
            $item = Product::getEntity([
                'uuid' => $uuid
            ]);
        }

        $options = [
            'builder' => $item->group_prices()
        ];

        $list = UserGroupPrice::getList($options);

        return $list->generateListResponse();
    }

    // 设置人群价
    public function store(Request $req, $type, $uuid)
    {
        $rule = [
            // 'list' => ['required', 'array']
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $list = $req->input('list') ?: [];

        if ($type === 'products') {
            $item = Product::getEntity([
                'uuid' => $uuid
            ]);
        }

        $item->setGroupPrices($list);

        return [];
    }
}
